package 题目集.hash.字符串哈希;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
//https://www.dotcpp.com/oj/contest6265_problem3.html
public class 破解密码 {
    /**
     * 求出母字符串所有位置的长度为8的hash值
     * 然后对字符串进行全排列，将排列后的字符串的hash值与母字符串的hash值进行比较，成功则答案++
     * @param args
     */
    static Map<Long, Integer> map = new HashMap<>();   //出现的次数
    static long[] base = new long[9];
    static int p = 131;

    static {
        base[0] = 1;
        for (int i = 1; i < base.length; i++) {
            base[i] = base[i - 1] * p;
        }
    }

    public static void main(String[] args) {
        Reader sc = new Reader();
        char[] chars = sc.nextLine().toCharArray();
        long pre = hash(chars, 0, 7);
        map.put(pre, 1);

        for (int i = 1; i + 7 < chars.length; i++) {
            long hash = (pre - chars[i - 1] * base[7]) * p + chars[i + 7];
            map.put(hash, map.getOrDefault(hash, 0) + 1);
            pre = hash;
        }
        int m = sc.nextInt();
        long res=0;
        for (int i = 0; i < m; i++) {
            char[] target = sc.nextLine().toCharArray();
            res+=dfs(target, 0);
        }
        System.out.println(res);
    }

    static Set<Long> vis = new HashSet<>();
    static char[] cur = new char[8];
    static boolean[] cvis = new boolean[8];

    public static long dfs(char[] chars, int i) {
        if (i == chars.length) {
            long hash = hash(cur, 0, 7);
            if (!vis.contains(hash)) {
                vis.add(hash);
                return map.getOrDefault(hash,0);
            }
        }
        long res = 0;
        for (int j = 0; j < chars.length; j++) {
            if (cvis[j]) {
                continue;
            }
            cur[i] = chars[j];
            cvis[j] = true;
            res += dfs(chars, i + 1);
            cvis[j] = false;
        }
        return res;
    }

    public static long hash(char[] chars, int l, int r) {
        long res = 0;
        while (l <= r) {
            res = res * p + chars[l++];
        }
        return res;
    }
    static
    class Reader {
        private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer tokenizer = new StringTokenizer("");

        private String innerNextLine() {
            try {
                return reader.readLine();
            } catch (IOException ex) {
                return null;
            }
        }

        public boolean hasNext() {
            while (!tokenizer.hasMoreTokens()) {    //内存中没有
                String nextLine = innerNextLine();
                if (nextLine == null) {     //且没有下一行
                    return false;
                }
                tokenizer = new StringTokenizer(nextLine);  //读入一行
            }
            return true;
        }

        public String nextLine() {
            tokenizer = new StringTokenizer("");    //清空内存中的字符
            return innerNextLine();
        }

        public void clear() {
            tokenizer = new StringTokenizer("");
        }

        public String next() {
            hasNext();
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
//    public BigInteger nextBigInt() {
//        return new BigInteger(next());
//    }
    }

}
